<!DOCTYPE html>
<html lang="en">
<head>
    <title>仓位在线</title>
    {% include 'admin/common/header.html' %}
    <link rel="stylesheet" href="{{ url_for('static', filename='admin/admin/css/other/user.css') }}"/>
</head>
<body class="pear-container">
{# 查询表单 #}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="mail-query-form">
            <div class="layui-form-item">
                <label class="layui-form-label">切片日期</label>
                <div class="layui-input-inline">
                    <select name="createDate" id="createDate">
                        {% for createDate in create_dates %}
                            <option value="{{ createDate }}">{{ createDate }}</option>
                        {% endfor %}
                    </select>
                </div>
                <label class="layui-form-label">指数类型</label>
                <div class="layui-input-inline">
                    <select name="ttype" id="ttype">
                        <option value="">请选择</option>
                        <option value="3">行业指数</option>
                        <option value="2">策略指数</option>
                        <option value="1">宽基指数</option>
                    </select>
                </div>
                <label class="layui-form-label">估值类型</label>
                <div class="layui-input-inline">
                    <select name="evaType" id="evaType">
                        <option value="">请选择</option>
                        <option value="low">偏低</option>
                        <option value="mid">适中</option>
                        <option value="high">偏高</option>
                    </select>
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="mail-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>

        </form>
    </div>
</div>
{# 用户表格 #}
<div class="user-main user-collasped">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="mail-table" lay-filter="mail-table"></table>
        </div>
    </div>
</div>
</body>
{# 表格操作 #}
<script type="text/html" id="mail-toolbar">
    {% if authorize("admin:mail:add") %}
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="pear-icon pear-icon-add"></i>
            新增
        </button>
    {% endif %}
    {% if authorize("admin:mail:remove") %}
        <button class="pear-btn pear-btn-md" lay-event="batchRemove">
            <i class="pear-icon pear-icon-ashbin"></i>
            删除
        </button>
    {% endif %}
</script>

{% include 'admin/common/footer.html' %}

<script>
    layui.use(['table', 'form', 'jquery', 'popup', 'common'], function () {
        let table = layui.table
        let form = layui.form
        let $ = layui.jquery
        let dtree = layui.dtree
        let popup = layui.popup
        let common = layui.common
        let MODULE_PATH = '/indexEva/'

        // 表格数据
        let cols = [
            [
                {field: 'index_code', title: '指数代码', align: 'center'},
                {
                    field: 'name', title: '指数名称', align: 'center', templet: function (d) {
                        return '<a href="' + d.fund_url + '" target="_blank">' + d.name + '</a>'
                    }
                },
                {field: 'create_date', title: '指数估值时间', align: 'center'},
                {
                    field: 'ttype', title: '指数类型', align: 'center', templet: function (d) {
                        if (d.ttype === '3') {
                            return '行业指数'
                        } else if (d.ttype === '2') {
                            return '策略指数'
                        } else if (d.ttype === '1') {
                            return '宽基指数'
                        }
                    }
                },
                {field: 'pe', title: 'pe', align: 'center'},
                {field: 'pb', title: 'pb', align: 'center'},
                {field: 'pe_percentile', title: 'pe百分位', align: 'center'},
                {field: 'pb_percentile', title: 'pb百分位', align: 'center'},
                {field: 'roe', title: '净资产收益率', align: 'center'},
                {field: 'yeild', title: '股息率', align: 'center'},
                {field: 'peg', title: '预测 peg', align: 'center'},
                {
                    field: 'eva_type', title: '指数估值类型', align: 'center', templet: function (d) {
                        if (d.eva_type === 'low') {
                            return '偏低'
                        } else if (d.eva_type === 'mid') {
                            return '适中'
                        } else if (d.eva_type === 'high') {
                            return '偏高'
                        } else {
                            return '未知'
                        }
                    }
                },
                {field: 'bond_yeild', title: '债权收益率', align: 'center'}
            ]
        ]

        // 渲染表格数据
        table.render({
            elem: '#mail-table',
            url: MODULE_PATH + 'data',
            page: true,
            limit: 15,
            cols: cols,
            skin: 'line',
            height: 'full-148',
            toolbar: '#mail-toolbar', /*工具栏*/
            text: {none: '暂无数据'},
            defaultToolbar: [{layEvent: 'refresh', icon: 'layui-icon-refresh'}, 'filter', 'print', 'exports'], /*默认工具栏*/
            done: function (res, curr, count) {
                $.each(res.data, function (index, item) {
                    if (item.eva_type === 'low') {
                        $('td[data-field=eva_type][data-content=low]').css('background-color', '#0fae9d')
                    } else if (item.eva_type === 'mid') {
                        $('td[data-field=eva_type][data-content=mid]').css('background-color', '#e5a43a')
                    } else if (item.eva_type === 'high') {
                        $('td[data-field=eva_type][data-content=high]').css('background-color', '#fe5a4b')
                    }
                })
            }
        })


        table.on('tool(mail-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj)
            }
        })

        table.on('toolbar(mail-table)', function (obj) {
            if (obj.event === 'add') {
                window.add()
            } else if (obj.event === 'refresh') {
                window.refresh()
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj)
            }
        })

        form.on('submit(mail-query)', function (data) {
            window.refresh(data.field)
            return false
        })

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['550px', '550px'],
                content: MODULE_PATH + 'add'
            })
        }


        window.remove = function (obj) {
            layer.confirm('确定要删除', {icon: 3, title: '提示'}, function (index) {
                layer.close(index)
                let loading = layer.load()
                $.ajax({
                    url: MODULE_PATH + 'remove/' + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading)
                        if (result.success) {
                            popup.success(result.msg, function () {
                                obj.del()
                            })
                        } else {
                            popup.failure(result.msg)
                        }
                    }
                })
            })
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data
            if (data.length === 0) {
                layer.msg('未选中数据', {
                    icon: 3,
                    time: 1000
                })
                return false
            }
            var ids = []
            var hasCheck = table.checkStatus('mail-table')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    ids.push(element.id)
                })
            }
            {#console.log(ids);#}
            layer.confirm('确定要删除选中数据', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index)
                let loading = layer.load()
                $.ajax({

                    url: MODULE_PATH + 'batchRemove',
                    data: {ids: ids},
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading)
                        if (result.success) {
                            popup.success(result.msg, function () {
                                table.reload('mail-table')
                            })
                        } else {
                            popup.failure(result.msg)
                        }
                    }
                })
            })
        }

        window.refresh = function (param) {
            table.reload('mail-table', {where: param})
        }
    })
</script>
</html>